<!--
  * 申请记录
  *
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-card size="small" :bordered="false" :hoverable="false" class="query-form-wrapper smart-margin-bottom10 smart-radius-2">
    <a-form>
      <a-row :gutter="[8, 10]">
        <a-col :span="5.5">
          <a-form-item label="申请日期" class="smart-margin-bottom0">
            <custom-range-picker v-model:value="queryForm.create_time" />
          </a-form-item>
        </a-col>
        <a-col :span="5.5">
          <a-form-item label="审核日期" class="smart-margin-bottom0">
            <custom-range-picker v-model:value="queryForm.update_time" />
          </a-form-item>
        </a-col>
        <a-col :span="3">
          <a-form-item label="用款周期" class="smart-margin-bottom0">
            <a-select ref="select" v-model:value="queryForm.loan_cycle" placeholder="请选择用款周期">
              <a-select-option value="1">3个月</a-select-option>
              <a-select-option value="2">6个月</a-select-option>
              <a-select-option value="3">1年</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="3">
          <a-form-item label="审核状态" class="smart-margin-bottom0">
            <a-select ref="select" v-model:value="queryForm.apply_status" placeholder="请选择审核状态">
              <a-select-option value="1">待审核</a-select-option>
              <a-select-option value="2">通过</a-select-option>
              <a-select-option value="3">驳回</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="5">
          <a-button-group>
            <a-button type="primary" html-type="submit" @click="onSearch" v-privilege="'goods:query'">
              <template #icon>
                <SearchOutlined />
              </template>
              查询
            </a-button>
            <a-button @click="resetQuery" v-privilege="'goods:query'">
              <template #icon>
                <ReloadOutlined />
              </template>
              重置
            </a-button>
          </a-button-group>
        </a-col>
      </a-row>
    </a-form>
  </a-card>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true" class="query-table-wrapper">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block">
        <a-button @click="onExport" type="primary" v-privilege="'goods:exportGoods'">
          <template #icon>
            <ExportOutlined />
          </template>
          导出
        </a-button>
      </div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.ERP.GOODS" :refresh="queryData" />
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->
    <div class="table-wrapper">
      <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :scroll="{ x: 'max-content', y: tableScrollY }" :pagination="false" :showSorterTooltip="false" :loading="tableLoading" :row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange }">
        <template #bodyCell="{ text, record, column }">
          <template v-if="column.dataIndex === 'loan_cycle'">
            <a-tag :color="TagColor[text]">{{ getDescByValue[Number(text)] }}</a-tag>
          </template>
          <template v-if="column.dataIndex === 'apply_status'">
            <a-tag :color="TagColor[text]">{{ UNSETYLED_TY_ENUM[Number(text)] }}</a-tag>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <div class="smart-table-operate">
              <a-button @click="detail(record)" type="link" style="color: #97999c" v-privilege="'goods:update'">详情</a-button>
            </div>
          </template>
        </template>
      </a-table>
    </div>

    <div class="smart-query-table-page query-table-page">
      <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS" :defaultPageSize="queryForm.limit" v-model:current="queryForm.page" v-model:pageSize="queryForm.limit" :total="total" @change="queryData" @showSizeChange="queryData" :show-total="(total) => `共${total}条`" />
    </div>
  </a-card>
</template>
<script setup lang="ts">
import CustomRangePicker from '@/components/business/custom-range-picker/index.vue'
import { applyApi } from '@/api/business/service/support-api'
import { PAGE_SIZE_OPTIONS, DEFAULT_TIME_RANGE } from '@/constants/system/common-const'
import TableOperator from '@/components/support/table-operator/index.vue'
import { TABLE_ID_CONST } from '@/constants/support/table-id-const'
import _ from 'lodash'
import { useQuery } from '@/hooks/web/use-query-hook'
import { QUERY_FORM_OP } from '@/constants/support/query-form-op-const'
import { useResize } from '@/hooks/web/use-resize-hook'

// ----------------------------- tag状态 -----------------------------
enum TagColor {
  'error' = 1,
  'orange',
  'success',
  'processing',
  'warning',
  'default'
}
enum getDescByValue {
  '3个月' = 1,
  '6个月',
  '1年'
}

enum UNSETYLED_TY_ENUM {
  '待审核' = 1,
  '通过',
  '驳回'
}
// ----------------------------- 计算表格高度 -----------------------------
const { tableScrollY } = useResize()

// ---------------------------- 表格列 ----------------------------

const columns = computed(() => [
  {
    title: '申请金额',
    dataIndex: 'apply_amount'
  },
  {
    title: '用款周期',
    dataIndex: 'loan_cycle'
  },
  {
    title: '审批状态',
    dataIndex: 'apply_status'
  },
  {
    title: '申请日期',
    dataIndex: 'create_time'
  },
  {
    title: '审批日期',
    dataIndex: 'update_time'
  }
])
// ---------------------------- 查询数据表单和方法 ----------------------------

const { queryForm, queryFormOp, tableData, tableLoading, total, resetQuery, onSearch, queryData } = useQuery({
  fetchDataApi: async (params) => {
    const res = await applyApi.queryList(params)
    return {
      list: res.data.data,
      total: res.data.total
    }
  },
  queryFormFilter: {
    create_time: DEFAULT_TIME_RANGE.value,
    update_time: DEFAULT_TIME_RANGE.value,
    loan_cycle: undefined,
    apply_status: undefined
  },
  queryFormOp: {
    create_time: QUERY_FORM_OP['范围查询'],
    update_time: QUERY_FORM_OP['范围查询'],
    loan_cycle: QUERY_FORM_OP['全等查询'],
    apply_status: QUERY_FORM_OP['全等查询']
  }
})

onActivated(queryData)

// ---------------------------- 详情 ----------------------------
const detailRef = ref()

const detail = (goodsData) => {
  detailRef.value.showDrawer(goodsData)
}

// ---------------------------- 批量 ----------------------------

// 选择表格行
const selectedRowKeyList = ref([])

function onSelectChange(selectedRowKeys) {
  selectedRowKeyList.value = selectedRowKeys
}
// ------------------------------- 导出和导入 ---------------------------------
// 导出
function onExport() {
  const { page, limit, ...filter } = queryForm.value
  const data = {
    filter: JSON.stringify(filter),
    op: JSON.stringify(queryFormOp)
  }
  applyApi.exportXlsx(data)
}
</script>
<style scoped lang="less">
:deep(.ant-badge .ant-badge-count) {
  height: 16px;
  line-height: 16px;
  font-size: 10px;
}
</style>
